//
//  DownloadContractViewModel.m
//  GLGW_Shoping
//
//  Created by whx on 2018/12/24.
//  Copyright © 2018年 yang. All rights reserved.
//

#import "DownloadContractViewModel.h"
#import "AlertInputView.h"
//#import <Photos/PHPhotoLibrary.h>
#import "GLGWPopTosView.h"

@interface DownloadContractViewModel() 

@end

@implementation DownloadContractViewModel

- (instancetype)initWithSuperView:(UIView *)superView
{
    if (self = [super initWithSuperView:superView]) {
        superView.backgroundColor = [UIColor colorWithHexString:@"#FAFAFA"];
        
        GLGWView *baView = [[GLGWView alloc] initWithInSuperView:superView];
        baView.backgroundColor = [UIColor colorWithHexString:@"#000000" alpha:0.5];
        [baView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.right.equalTo(superView);
            make.bottom.equalTo(superView.mas_bottom).offset(-kHeightProportion_In_iPhoneTriploid(34));
        }];
        
        GLGWView *bottomV = [[GLGWView alloc] initWithInSuperView:baView];
        bottomV.backgroundColor = [UIColor colorWithHexString:@"#FFFFFF"];
        [bottomV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.equalTo(baView);
            make.height.equalTo(@(kHeightProportion_In_iPhoneTriploid(97)));
        }];
        
        GLGWButton *middleBtn = [[GLGWButton alloc] initWithTitle:@"发送到微信" titleLableFont:SYSTEMFONT(12) titleLableColor:[UIColor colorWithHexString:@"#666666"] inSuperView:bottomV];
        [middleBtn setImage:[UIImage imageNamed:@"icon_weixinfenxiang"] forState:UIControlStateNormal];
        [middleBtn setImagePositionWithType:SSImagePositionTypeTop spacing:5];
        [middleBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(bottomV.mas_centerX);
            make.centerY.equalTo(bottomV.mas_centerY);
            make.width.equalTo(@(kWidthProportion_In_iPhoneTriploid(65)));
            make.height.equalTo(@(kWidthProportion_In_iPhoneTriploid(67)));
        }];
        [middleBtn addTarget:self action:@selector(sendWeiXinAction:) forControlEvents:UIControlEventTouchUpInside];
        
        GLGWButton *leftBtn = [[GLGWButton alloc] initWithTitle:@"保存图片" titleLableFont:SYSTEMFONT(12) titleLableColor:[UIColor colorWithHexString:@"#666666"] inSuperView:bottomV];
        [leftBtn setImage:[UIImage imageNamed:@"icon_baocuntupian"] forState:UIControlStateNormal];
        [leftBtn setImagePositionWithType:SSImagePositionTypeTop spacing:5];
        [leftBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(middleBtn.mas_left).offset(-kWidthProportion_In_iPhoneTriploid(40));
            make.centerY.equalTo(bottomV.mas_centerY);
            make.width.equalTo(@(kWidthProportion_In_iPhoneTriploid(65)));
            make.height.equalTo(@(kWidthProportion_In_iPhoneTriploid(67)));
        }];
        [leftBtn addTarget:self action:@selector(keepPic:) forControlEvents:UIControlEventTouchUpInside];

        GLGWButton *rightBtn = [[GLGWButton alloc] initWithTitle:@"发送到邮箱" titleLableFont:SYSTEMFONT(12) titleLableColor:[UIColor colorWithHexString:@"#666666"] inSuperView:bottomV];
        [rightBtn setImage:[UIImage imageNamed:@"icon_fasongyouxiang"] forState:UIControlStateNormal];
        [rightBtn setImagePositionWithType:SSImagePositionTypeTop spacing:5];
        [rightBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(middleBtn.mas_right).offset(kWidthProportion_In_iPhoneTriploid(40));
            make.centerY.equalTo(bottomV.mas_centerY);
            make.width.equalTo(@(kWidthProportion_In_iPhoneTriploid(65)));
            make.height.equalTo(@(kWidthProportion_In_iPhoneTriploid(67)));
        }];
        [rightBtn addTarget:self action:@selector(sendEmail:) forControlEvents:UIControlEventTouchUpInside];

        GLGWImageView *imgV = [[GLGWImageView alloc] initInSuperView:baView];
        [imgV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(baView.mas_top).offset(kHeightProportion_In_iPhoneTriploid(108));
            make.left.equalTo(baView.mas_left).offset(kWidthProportion_In_iPhoneTriploid(37));
            make.right.equalTo(baView.mas_right).offset(-kWidthProportion_In_iPhoneTriploid(38));
            make.bottom.equalTo(bottomV.mas_top).offset(-kHeightProportion_In_iPhoneTriploid(50));
        }];
        imgV.backgroundColor = kRandomColor;
        
        GLGWButton *backBtn = [[GLGWButton alloc] initInSuperView:baView];
        [backBtn setImage:[UIImage imageNamed:@"icon_fanhui"] forState:UIControlStateNormal];
        [backBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.height.equalTo(@(kWidthProportion_In_iPhoneTriploid(48)));
            make.left.equalTo(baView.mas_left);
            make.top.equalTo(baView.mas_top).offset(kHeightProportion_In_iPhoneTriploid(38));
        }];
        [backBtn addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
        
    }
    return self;
}

- (void)sendWeiXinAction:(GLGWButton *)sender
{
    if (self.BlockShareWX) {
        self.BlockShareWX([UIImage imageNamed:@"icon_daizhifu"]);
    }
}

- (void)keepPic:(GLGWButton *)sender
{
    [self loadImageFinished:[UIImage imageNamed:@"icon_daizhifu"]];
}

- (void)sendEmail:(GLGWButton *)sender
{
    AlertInputView *alertInputV = [[AlertInputView alloc] initAlertInputView:@[@"取消", @"确认发送"] titleStr:@"发送到邮箱" placeHolderStr:@"请输入正确的邮箱地址" keyType:UIKeyboardTypeEmailAddress];
    kWeakSelf(self);
    alertInputV.Block_CaseAction = ^(NSInteger tag, NSString *textFStr) {
        if (tag == 311) {
            [weakself postEmail:textFStr];
        }
    };
}

- (void)backAction:(GLGWButton *)sender
{
    if (self.BlockBackBtn) {
        self.BlockBackBtn();
    }
}

- (void)postEmail:(NSString *)emailStr
{
    // 弹出邮件发送视图
    if (self.BlockSendEmail) {
        self.BlockSendEmail(emailStr);
        //        [self presentViewController:mailCompose animated:YES completion:nil];
    }
}

// 下载图片
- (void)loadImageFinished:(UIImage *)image
{
    UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), (__bridge void *)self);
}
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
    DLog(@"image = %@, error = %@, contextInfo = %@", image, error, contextInfo);
    BOOL isYes = YES;
    if (error) {
        isYes = NO;
    }
    [self keepAlet:isYes];
}

- (void)keepAlet:(BOOL)isKeep
{
    if (isKeep) {
        GLGWPopTosView *p_View2 = [[GLGWPopTosView alloc] initWithPopView:PopViewType_Success];
        p_View2.tosTitle = @"保存成功!";
    } else {
        GLGWPopTosView *p_View2 = [[GLGWPopTosView alloc] initWithPopView:PopViewType_Failure];
        p_View2.tosTitle = @"保存失败!";
    }
}


@end
